package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.ExceptionUtil;
import com.ruoyi.system.dao.DmsValueDao;
import com.ruoyi.system.domain.dto.AmsFirstAidDto;
import com.ruoyi.system.domain.dto.DmsValueDto;
import com.ruoyi.system.domain.po.*;
import com.ruoyi.system.domain.vo.DmsValueVo;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.AmsFirstAidService;
import com.ruoyi.system.service.DmsDataValueService;
import com.ruoyi.system.service.DmsRunningLogService;
import com.ruoyi.system.service.TbDataValueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;

import static com.ruoyi.common.constant.UserConstants.QuestionCode.ZL_JBXX_ZYH;
import static com.ruoyi.common.enums.DataSourceType.MASTER;

@Slf4j
@Service
public class DmsDataValueServiceImpl implements DmsDataValueService {
    public static final String DATE_FORMAT = "yyyy-MM-dd";
    @Resource
    public DmsValueDao dmsValueDao;
    @Resource
    public DmsSsjlMapper dmsSsjlMapper;
    @Resource
    public DmsCyMapper dmsCyMapper;
    @Resource
    public DmsPatientMapper dmsPatientMapper;
    @Resource
    public DmsZdxxMapper dmsZdxxMapper;
    @Resource
    public DmsCtMapper ctMapper;
    @Resource
    public PmsArchivesMapper pmsArchivesMapper;
    @Resource
    public AmsFirstAidService amsFirstAidService;
    @Resource
    public DmsJianyanMapper dmsJianyanMapper;
    @Resource
    public DmsYizhuMapper yizhuMapper;
    @Resource
    public PmsArchivesRecordMapper pmsArchivesRecordMapper;
    @Resource
    public DmsRunningLogMapper dmsRunningLogMapper;
    @Resource
    public TbBaFyxxMapper tbBaFyxxMapper;
    @Resource
    public TbBaFzxxMapper tbBaFzxxMapper;
    @Resource
    public TbBaIcuxxMapper tbBaIcuxxMapper;
    @Resource
    public TbBaJbxxMapper tbBaJbxxMapper;
    @Resource
    public TbBaSsxxMapper tbBaSsxxMapper;
    @Resource
    public TbBaZdxxMapper tbBaZdxxMapper;
    @Resource
    public TbBaZyrbMapper tbBaZyrbMapper;
    @Resource
    public TbZyYzMapper tbZyYzMapper;
    @Resource
    public TbBl24HrcyjlxxMapper tbBl24HrcyjlxxMapper;
    @Resource
    public TbBlCyxjMapper tbBlCyxjMapper;
    @Resource
    public TbBlGzhcsyjlhcMapper tbBlGzhcsyjlhcMapper;
    @Resource
    public TbBlGzhcsyjlxxMapper tbBlGzhcsyjlxxMapper;
    @Resource
    public TbBlLcljjlxxMapper tbBlLcljjlxxMapper;
    @Resource
    public TbBlMjzjbxxMapper tbBlMjzjbxxMapper;
    @Resource
    public TbBlMjzyzxxMapper tbBlMjzyzxxMapper;
    @Resource
    public TbBlMjzzdxxMapper tbBlMjzzdxxMapper;
    @Resource
    public TbBlMzblgyMapper tbBlMzblgyMapper;
    @Resource
    public TbHzxxbMapper tbHzxxbMapper;
    @Resource
    public TbBlRybljlMapper tbBlRybljlMapper;
    @Resource
    public TbBlScbcjlxxMapper tbBlScbcjlxxMapper;
    @Resource
    public TbBlSwjlxxMapper tbBlSwjlxxMapper;
    @Resource
    public TbBlZyblgyMapper tbBlZyblgyMapper;
    @Resource
    public TbJcJcwdcjMapper tbJcJcwdcjMapper;
    @Resource
    public TbJcYxjcbgbMapper tbJcYxjcbgbMapper;
    @Resource
    public TbJyJybgzbMapper tbJyJybgzbMapper;
    @Resource
    public TbJyJyjgzbbMapper tbJyJyjgzbbMapper;
    @Resource
    public TbJyJywdcjMapper tbJyJywdcjMapper;
    @Resource
    public TbJyXjjgbMapper tbJyXjjgbMapper;
    @Resource
    public TbJyYmjgbMapper tbJyYmjgbMapper;
    @Resource
    public TbMzGhmxbMapper tbMzGhmxbMapper;
    @Resource
    public TbSsSsjlbMapper tbSsSsjlbMapper;
    @Resource
    public TbSsSsmxbMapper tbSsSsmxbMapper;
    @Resource
    private MtPatientProgressMapper patientProgressMapper;
    @Resource
    public TbDataValueService tbDataValueService;
    @Resource
    public PmsPatientMapper pmsPatientMapper;
    @Resource
    public DmsRunningLogService dmsRunningLogService;
    @Resource
    public BlsZlInfoMapper blsZlInfoMapper;
    @Resource
    public BlsZlInfoRecordMapper zlInfoRecordMapper;
    @Resource
    private TbZyRydjmxbMapper tbZyRydjmxbMapper;
    @Resource
    private TbZyJsmxbMapper tbZyJsmxbMapper;
    @Resource
    private TbZyCydjmxbMapper tbZyCydjmxbMapper;

    @Override
    @DataSource(DataSourceType.MASTER)
    public void pulling() {
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void insert(Date startDate, Date endDate, Date lastSyncDate) {
        log.info("数据拉取：{},{},{}", DateUtil.date(startDate).toString(DATE_FORMAT), DateUtil.date(endDate).toString(DATE_FORMAT), lastSyncDate);

        while (DateUtil.compare(startDate, endDate) <= 0) {
            DmsRunningLog dcsRunningLog = newDcsRunningLog(UserConstants.DcsRunningStatus.Processing.getCode().byteValue());
            dmsRunningLogService.insertRunningLog(dcsRunningLog);
            try {
                //任务执行
                extracted(startDate, endDate);
                //更新lastMakeupDate
                if (startDate.compareTo(lastSyncDate) > 0) {
                    lastSyncDate = startDate;
                }
                //设置状态
                dcsRunningLog.setStatus(UserConstants.DcsRunningStatus.Success.getCode().byteValue());
                //日期+1
                startDate = DateUtil.offset(startDate, DateField.DAY_OF_YEAR, 1);
            } catch (Exception e) {
                //设置状态
                log.error("error: ", e);
                String remark = ExceptionUtil.getExceptionMessage(e);
                if (StrUtil.isNotEmpty(remark) && remark.length() > 1024) {
                    remark = remark.substring(0, 1024);
                }
                dcsRunningLog.setRemark(remark);
                dcsRunningLog.setStatus(UserConstants.DcsRunningStatus.Failed.getCode().byteValue());
                throw e;
            } finally {
                //更新日志
                dcsRunningLog.setEndTime(DateUtil.date());
                dcsRunningLog.setLastSyncDate(lastSyncDate);
                dmsRunningLogService.updateRunningLog(dcsRunningLog);
            }
        }
    }

    public void extracted(Date startDate, Date endDate) {
        //住院治疗档案
        Map<String, BlsZlInfo> zlInfoMap = new HashMap<>();
        List<BlsZlInfoRecord> recordList = new ArrayList<>();
        List<String> jzlshList = new ArrayList<>();
        //查询 TB_BA_ZDXX住院病案诊断信息表，ZDBM诊断代码为I60.*、I61.*、I63.*的数据，获取到住院就诊流水号
        List<DmsValueVo.Zdxx> zdxxList = tbDataValueService.queryZdxx(startDate, endDate, jzlshList);

        List<DmsValueDto.Hzxx> hzxxList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(zdxxList)) {
            insertZdxx(zdxxList);
            jzlshList.addAll(zdxxList.stream().map(DmsValueVo.Zdxx::getJzlsh).collect(Collectors.toList()));
            for (DmsValueVo.Zdxx zdxx : zdxxList) {
                DmsValueDto.Hzxx hzxx = new DmsValueDto.Hzxx();
                hzxx.setGrbslx(zdxx.getGrbslx());
                hzxx.setGrbsm(zdxx.getGrbsm());
                hzxxList.add(hzxx);

                //解析  ZL-JBXX-RYTJ	入院途径
                //ZL-JBXX-ZDMC	主要诊断名称
                //ZL-JBXX-ZDBM	主要诊断编码
                BlsZlInfo blsZlInfo = new BlsZlInfo();
                blsZlInfo.setId(IdUtil.getSnowflake().nextId());
                blsZlInfo.setGrbslx(zdxx.getGrbslx());
                blsZlInfo.setGrbsm(zdxx.getGrbsm());
                blsZlInfo.setZdmc(zdxx.getZdmc());
                blsZlInfo.setZdbm(zdxx.getZdbm());
                blsZlInfo.setJzlsh(blsZlInfo.getJzlsh());
                zlInfoMap.put(blsZlInfo.getJzlsh(), blsZlInfo);
            }
        }
        //查询TB_BA_SSXX住院病案手术信息表，SSJCZBM手术及操作代码字段为附1的 数据，获取到住院就诊流水号；
        List<DmsValueVo.Ssxx> ssxxList = tbDataValueService.querySsxx(startDate, endDate, jzlshList);
        if (CollectionUtil.isNotEmpty(ssxxList)) {
            insertSsxx(ssxxList);
            jzlshList.addAll(ssxxList.stream().map(DmsValueVo.Ssxx::getJzlsh).collect(Collectors.toList()));
            for (DmsValueVo.Ssxx ssxx : ssxxList) {
                DmsValueDto.Hzxx hzxx = new DmsValueDto.Hzxx();
                hzxx.setGrbslx(ssxx.getGrbslx());
                hzxx.setGrbsm(ssxx.getGrbsm());
                hzxxList.add(hzxx);

                BlsZlInfo blsZlInfo = new BlsZlInfo();
                blsZlInfo.setId(IdUtil.getSnowflake().nextId());
                blsZlInfo.setGrbslx(ssxx.getGrbslx());
                blsZlInfo.setGrbsm(ssxx.getGrbsm());
//                blsZlInfo.setZdmc(ssxx.getZDMC());
//                blsZlInfo.setZdbm(ssxx.getZDBM());
                blsZlInfo.setJzlsh(blsZlInfo.getJzlsh());
                zlInfoMap.put(blsZlInfo.getJzlsh(), blsZlInfo);
            }
        }

        Map<String, List<PmsPatient>> patientMap = new HashMap<>();
        //患者信息
        List<DmsValueVo.Patient> patients = tbDataValueService.queryHzxx(hzxxList);
        if (CollectionUtil.isNotEmpty(patients)) {
            List<TbHzxxb> tbHzxxbList = BeanUtil.copyToList(patients, TbHzxxb.class);
            List<PmsPatient> patientList = insertHzxx(tbHzxxbList);
            patientMap = patientList.stream().collect(Collectors.groupingBy(PmsPatient::getGrbslx));
        }
        List<DmsValueVo.Zyblgy> zyblgyList = tbDataValueService.queryZyblgy(startDate, endDate, jzlshList);
        if (CollectionUtil.isNotEmpty(zyblgyList)) {
            insertZyblgy(zyblgyList);
        }
        List<DmsValueVo.Rybljl> rybljlList = tbDataValueService.queryRybljl(startDate, endDate, jzlshList);
        if (CollectionUtil.isNotEmpty(rybljlList)) {
            insertRybljl(rybljlList);
            //ZL-CEACAS-SS-FS	手术采取方式   ZL-RYPG-CEACAS-JC	颈部血管检查
            for (DmsValueVo.Rybljl rybljl : rybljlList) {
                BlsZlInfo blsZlInfo = zlInfoMap.get(rybljl.getJzlsh());
                if (blsZlInfo == null) {
                    continue;
                }
//                BlsZlInfoRecord blsZlInfoRecord = new BlsZlInfoRecord();
//
//                blsZlInfoRecord.setAnswer(rybljl.getJbjcjg());
//                blsZlInfoRecord.setCode("ZL-RYPG-CEACAS-JC");
//                blsZlInfoRecord.setId(IdUtil.getSnowflake().nextId());
//                blsZlInfoRecord.setBlId(blsZlInfo.getId());
//                blsZlInfoRecord.setDelFlag((byte) 0);
//                recordList.add(blsZlInfoRecord);
            }
        }
        List<DmsValueVo.Swjlxx> swjlxxList = tbDataValueService.querySwjlxx(startDate, endDate, jzlshList);
        if (CollectionUtil.isNotEmpty(swjlxxList)) {
            insertSwjlxx(swjlxxList);

            //ZL-JBXX-DIE-R	死亡原因
            for (DmsValueVo.Swjlxx swjlxx : swjlxxList) {
                BlsZlInfo blsZlInfo = zlInfoMap.get(swjlxx.getJzlsh());
                if (blsZlInfo == null) {
                    continue;
                }
                BlsZlInfoRecord blsZlInfoRecord = new BlsZlInfoRecord();
                blsZlInfoRecord.setAnswer(swjlxx.getZjswyymc());
                blsZlInfoRecord.setCode("ZL-JBXX-DIE-R");
                blsZlInfoRecord.setId(IdUtil.getSnowflake().nextId());
                blsZlInfoRecord.setBlId(blsZlInfo.getId());
                blsZlInfoRecord.setDelFlag((byte) 0);
                recordList.add(blsZlInfoRecord);
            }
        }
        List<DmsValueVo.Gzhcsyjlxx> gzhcsyjlxxList = tbDataValueService.queryGzhcsyjlxx(startDate, endDate, jzlshList);
        if (CollectionUtil.isNotEmpty(gzhcsyjlxxList)) {
            insertGzhcsyjlxx(gzhcsyjlxxList);
        }
        List<DmsValueVo.Gzhcsyjlhc> gzhcsyjlhcList = tbDataValueService.queryGzhcsyjlhc(startDate, endDate, jzlshList);
        if (CollectionUtil.isNotEmpty(gzhcsyjlhcList)) {
            insertGzhcsyjhc(gzhcsyjlhcList);
        }
        List<DmsValueVo.Hrcyjlxx> hrcyjlxxList = tbDataValueService.query24Hrcyjlxx(startDate, endDate, jzlshList);
        if (CollectionUtil.isNotEmpty(hrcyjlxxList)) {
            insertHrcyjlxx(hrcyjlxxList);
        }
        List<DmsValueVo.Scbcjlxx> scbcjlxxList = tbDataValueService.queryScbcjlxx(startDate, endDate, jzlshList);
        if (CollectionUtil.isNotEmpty(scbcjlxxList)) {
            insertScbcjlxx(scbcjlxxList);
        }
        List<DmsValueVo.Lcljjlxx> lcljjlxxList = tbDataValueService.queryLcljjlxx(startDate, endDate, jzlshList);
        if (CollectionUtil.isNotEmpty(lcljjlxxList)) {
            insertLcljjxx(lcljjlxxList);
        }
        List<DmsValueVo.Cyxj> cyxjList = tbDataValueService.queryCyxj(startDate, endDate, jzlshList);
        if (CollectionUtil.isNotEmpty(cyxjList)) {
            insertCyxj(cyxjList);
        }
        List<DmsValueVo.Jybgzb> jybgzbList = tbDataValueService.queryJybgzb(startDate, endDate, jzlshList);
        if (CollectionUtil.isNotEmpty(jybgzbList)) {
            insertJybgzb(jybgzbList);
        }
        List<DmsValueVo.Jyjgzbb> jyjgzbbList = tbDataValueService.queryJyjgzbb(startDate, endDate, jzlshList);
        if (CollectionUtil.isNotEmpty(jyjgzbbList)) {
            insertJyjgzbb(jyjgzbbList);
        }
        List<DmsValueVo.Xjjgb> xjjgbList = tbDataValueService.queryXjjgb(startDate, endDate, jzlshList);
        if (CollectionUtil.isNotEmpty(xjjgbList)) {
            insertXjjgb(xjjgbList);
        }
        List<DmsValueVo.Ymjgb> ymjgbList = tbDataValueService.queryYmjgb(startDate, endDate, jzlshList);
        if (CollectionUtil.isNotEmpty(ymjgbList)) {
            insertYmhgb(ymjgbList);
        }
        List<DmsValueVo.Jywdcj> jywdcjList = tbDataValueService.queryJywdcj(startDate, endDate, jzlshList);
        if (CollectionUtil.isNotEmpty(jywdcjList)) {
            insertJywdcj(jywdcjList);
        }
        List<DmsValueVo.Jcwdcj> jcwdcjList = tbDataValueService.queryJcwdcj(startDate, endDate, jzlshList);
        if (CollectionUtil.isNotEmpty(jcwdcjList)) {
            insertJcwdcj(jcwdcjList);
        }
        List<DmsValueVo.Jbxx> jbxxList = tbDataValueService.queryJbxx(startDate, endDate, jzlshList);
        if (CollectionUtil.isNotEmpty(jbxxList)) {
            insertJbxx(jbxxList);
            for (DmsValueVo.Jbxx jbxx : jbxxList) {
                BlsZlInfo blsZlInfo = zlInfoMap.get(jbxx.getJzlsh());
                if (blsZlInfo != null) {
                    //出院时间
                    blsZlInfo.setCysj(DateUtil.parse(jbxx.getCysj()));
                    //入院途径1：门诊入院；2：急诊入院；3：其他医疗机构转入；9：其他
                    blsZlInfo.setRytj(jbxx.getRylx());
                    blsZlInfo.setSjzyts(jbxx.getSjzyts());
                }
            }
        }
        List<DmsValueVo.Fzxx> fzxxList = tbDataValueService.queryFzxx(startDate, endDate, jzlshList);
        if (CollectionUtil.isNotEmpty(fzxxList)) {
            insertFzxx(fzxxList);
        }

        List<DmsValueVo.Fyxx> fyxxList = tbDataValueService.queryFyxx(startDate, endDate, jzlshList);
        if (CollectionUtil.isNotEmpty(fyxxList)) {
            insertFyxx(fyxxList);
            for (DmsValueVo.Fyxx fyxx : fyxxList) {
                BlsZlInfo blsZlInfo = zlInfoMap.get(fyxx.getJzlsh());
                if (blsZlInfo == null) {
                    continue;
                }
                blsZlInfo.setZfy(fyxx.getZfy());
                zlInfoMap.put(blsZlInfo.getJzlsh(), blsZlInfo);
            }
        }
        List<DmsValueVo.Icuxx> icuxxList = tbDataValueService.queryIcuxx(startDate, endDate, jzlshList);
        if (CollectionUtil.isNotEmpty(icuxxList)) {
            insertIcuxx(icuxxList);
        }
        List<DmsValueVo.Zyrb> zyrbList = tbDataValueService.queryZyrb(startDate, endDate, jzlshList);
        if (CollectionUtil.isNotEmpty(zyrbList)) {
            insertZyrb(zyrbList);
        }
        List<DmsValueVo.Yizhu> yizhuList = tbDataValueService.getYizhu(jzlshList);
        if (CollectionUtil.isNotEmpty(yizhuList)) {
            insertZyYz(yizhuList);
            for (DmsValueVo.Yizhu yizhu : yizhuList) {
                BlsZlInfo blsZlInfo = zlInfoMap.get(yizhu.getJzlsh());
                if (blsZlInfo == null) {
                    continue;
                }
                BlsZlInfoRecord blsZlInfoRecord = new BlsZlInfoRecord();
                blsZlInfoRecord.setAnswer(yizhu.getJxdm());
                blsZlInfoRecord.setCode("ZL-JRZT-YWZL-DUAL");
                blsZlInfoRecord.setId(IdUtil.getSnowflake().nextId());
                blsZlInfoRecord.setBlId(blsZlInfo.getId());
                blsZlInfoRecord.setDelFlag((byte) 0);
                recordList.add(blsZlInfoRecord);

                BlsZlInfoRecord blsZlInfoRecord1 = new BlsZlInfoRecord();
                blsZlInfoRecord1.setAnswer(yizhu.getBah());
                blsZlInfoRecord1.setCode(ZL_JBXX_ZYH.getKey());
                blsZlInfoRecord1.setId(IdUtil.getSnowflake().nextId());
                blsZlInfoRecord1.setBlId(blsZlInfo.getId());
                blsZlInfoRecord1.setDelFlag((byte) 0);
                recordList.add(blsZlInfoRecord1);
            }
        }
        extracted(zlInfoMap, recordList, patientMap);
    }

    @DataSource(MASTER)
    @Override
    public void extracted(Map<String, BlsZlInfo> zlInfoMap, List<BlsZlInfoRecord> recordList, Map<String, List<PmsPatient>> patientMap) {
        for (Map.Entry<String, BlsZlInfo> entry : zlInfoMap.entrySet()) {
            BlsZlInfo value = entry.getValue();

            BlsZlInfoExample blsZlInfoExample = new BlsZlInfoExample();
            BlsZlInfoExample.Criteria criteria = blsZlInfoExample.createCriteria().andDelFlagEqualTo((byte) 0);
            if (StrUtil.isNotEmpty(value.getZyh())) {
                criteria.andZyhEqualTo(value.getZyh());
            }
            if (StrUtil.isNotEmpty(value.getJzlsh())) {
                criteria.andJzlshEqualTo(value.getJzlsh());
            }
            BlsZlInfo blsZlInfo = new BlsZlInfo();
            blsZlInfo.setDelFlag((byte) 1);

            blsZlInfoMapper.updateByExampleSelective(blsZlInfo, blsZlInfoExample);

            List<PmsPatient> tbHzxxbs = patientMap.get(value.getGrbslx());
            if (CollectionUtil.isNotEmpty(tbHzxxbs)) {
                Map<String, List<PmsPatient>> collect = tbHzxxbs.stream().collect(Collectors.groupingBy(PmsPatient::getGrbsm));
                List<PmsPatient> patientList = collect.get(value.getGrbsm());
                if (CollectionUtil.isNotEmpty(patientList)) {
                    Long id = patientList.get(0).getId();
                    String idCard = patientList.get(0).getIdCard();
                    value.setPatientId(id);
                    value.setProjectId(1792370919791734784L);
                    value.setSource(0);
                    value.setArchivestype("未提交");
                    try {
                        //获取出生日期
                        int ageByIdCard = IdcardUtil.getAgeByIdCard(idCard, value.getRysj());
                        value.setZynl(ageByIdCard);
                    }catch (Exception e){
                        log.error("计算年龄错误");
                    }
                    blsZlInfoMapper.insertSelective(value);
                }
            }
//            try {
//                //解析文书信息
//                List<BlsZlInfoRecord> mtPatientCodeList = getMtPatientCode(blsZlInfo);
//                if (CollectionUtil.isNotEmpty(mtPatientCodeList)) {
//                    recordList.addAll(mtPatientCodeList);
//                }
//            } catch (Exception e) {
//                log.error("解析病例文书Code错误：{},{}", value.getId(), value.getJzlsh());
//                log.error("错误：{}", e.getMessage());
//                e.printStackTrace();
//            }
        }
        for (BlsZlInfoRecord blsZlInfoRecord : recordList) {
            if (StrUtil.isEmpty(blsZlInfoRecord.getAnswer())) {
                continue;
            }
            BlsZlInfoRecordExample blsZlInfoRecordExample = new BlsZlInfoRecordExample();
            blsZlInfoRecordExample.createCriteria().andCodeEqualTo(blsZlInfoRecord.getCode()).andBlIdEqualTo(blsZlInfoRecord.getBlId());
            List<BlsZlInfoRecord> recordList1 = zlInfoRecordMapper.selectByExample(blsZlInfoRecordExample);
            if (CollectionUtil.isNotEmpty(recordList1)) {
                for (BlsZlInfoRecord zlInfoRecord : recordList1) {
                    zlInfoRecordMapper.deleteByPrimaryKey(zlInfoRecord.getId());
                }
            }
            zlInfoRecordMapper.insertSelective(blsZlInfoRecord);
        }
    }

    @DataSource(MASTER)
    public void insertZyrb(List<DmsValueVo.Zyrb> zyrbList) {
        List<TbBaZyrb> tbBlMzblgies = BeanUtil.copyToList(zyrbList, TbBaZyrb.class);
        for (TbBaZyrb tbBlMzblgy : tbBlMzblgies) {
            tbBaZyrbMapper.insertSelective(tbBlMzblgy);
        }
    }

    @DataSource(MASTER)
    public void insertZyYz(List<DmsValueVo.Yizhu> yizhuList) {
        List<TbZyYz> tbBlMzblgies = BeanUtil.copyToList(yizhuList, TbZyYz.class);
        for (TbZyYz tbBlMzblgy : tbBlMzblgies) {
            tbZyYzMapper.insertSelective(tbBlMzblgy);
        }
    }

    @Override
    @DataSource(MASTER)
    public void inserttbZyCydjmxbList(List<DmsValueVo.TbZyCydjmxb> tbZyCydjmxbList) {
        List<TbZyCydjmxb> tbBlMzblgies = BeanUtil.copyToList(tbZyCydjmxbList, TbZyCydjmxb.class);
        for (TbZyCydjmxb tbBlMzblgy : tbBlMzblgies) {
            tbZyCydjmxbMapper.insertSelective(tbBlMzblgy);
        }
    }

    @Override
    @DataSource(MASTER)
    public void insertTbBlMzblgies(List<DmsValueVo.TbBlMzblgy> tbBlMzblgies) {
        List<TbBlMzblgy> tbBlMzblgyList = BeanUtil.copyToList(tbBlMzblgies, TbBlMzblgy.class);
        for (TbBlMzblgy tbBlMzblgy : tbBlMzblgyList) {
            tbBlMzblgyMapper.insertSelective(tbBlMzblgy);
        }
    }

    @Override
    public void insertSsmx(List<DmsValueVo.TbSsSsmxb> tbSsSsmxbList) {
        List<TbSsSsmxb> tbBlMzblgyList = BeanUtil.copyToList(tbSsSsmxbList, TbSsSsmxb.class);
        for (TbSsSsmxb tbBlMzblgy : tbBlMzblgyList) {
            tbSsSsmxbMapper.insertSelective(tbBlMzblgy);
        }
    }

    @Override
    public void insertMtProgress(List<DmsValueVo.MtProgress> mtProgressesList) {
        for (DmsValueVo.MtProgress mtProgress : mtProgressesList) {
            MtPatientProgress mtPatientProgress = new MtPatientProgress();
            mtPatientProgress.setPatientGuid(mtProgress.getPatientGuid());
            mtPatientProgress.setProgressMessage(mtProgress.getProgressMessage());
            mtPatientProgress.setProgressType(mtProgress.getProgressType());
            mtPatientProgress.setProgressTemplateName(mtProgress.getProgressTemplateName());
            mtPatientProgress.setSerialNumber(mtProgress.getSerialNumber());
//            mtPatientProgress.setRecordTimeFormat(DateUtil.parse(mtProgress.getRecordTimeFormat(), "yyyy-MM-dd HH:mm:ss"));
            if (mtProgress.getProgressType().matches(".*静脉溶.*|.*溶栓.*") ||
            mtProgress.getProgressMessage().matches(".*静脉溶.*|.*溶栓.*")) {
                mtPatientProgress.setIsRs("1");
            }else{
                mtPatientProgress.setIsRs("0");
            }
            try {
                patientProgressMapper.insert(mtPatientProgress);
            }catch (Exception e){
                log.error("文书添加失败");
            }

        }
    }

    @DataSource(MASTER)
    public void insertIcuxx(List<DmsValueVo.Icuxx> icuxxList) {
        List<TbBaIcuxx> tbBlMzblgies = BeanUtil.copyToList(icuxxList, TbBaIcuxx.class);
        for (TbBaIcuxx tbBlMzblgy : tbBlMzblgies) {
            tbBaIcuxxMapper.insertSelective(tbBlMzblgy);
        }
    }

    @DataSource(MASTER)
    public void insertFyxx(List<DmsValueVo.Fyxx> fyxxList) {
        List<TbBaFyxx> tbBlMzblgies = BeanUtil.copyToList(fyxxList, TbBaFyxx.class);
        for (TbBaFyxx tbBlMzblgy : tbBlMzblgies) {
            tbBaFyxxMapper.insertSelective(tbBlMzblgy);
        }
    }

    @DataSource(MASTER)
    public void insertFzxx(List<DmsValueVo.Fzxx> fzxxList) {
        List<TbBaFzxx> tbBlMzblgies = BeanUtil.copyToList(fzxxList, TbBaFzxx.class);
        for (TbBaFzxx tbBlMzblgy : tbBlMzblgies) {
            tbBaFzxxMapper.insertSelective(tbBlMzblgy);
        }
    }

    @DataSource(MASTER)
    public void insertJbxx(List<DmsValueVo.Jbxx> jbxxList) {
        List<TbBaJbxx> tbBlMzblgies = BeanUtil.copyToList(jbxxList, TbBaJbxx.class);
        for (TbBaJbxx tbBlMzblgy : tbBlMzblgies) {
            tbBaJbxxMapper.insertSelective(tbBlMzblgy);
        }
    }

    @DataSource(MASTER)
    public void insertJcwdcj(List<DmsValueVo.Jcwdcj> jcwdcjList) {
        List<TbJcJcwdcj> tbBlMzblgies = BeanUtil.copyToList(jcwdcjList, TbJcJcwdcj.class);
        for (TbJcJcwdcj tbBlMzblgy : tbBlMzblgies) {
            TbJcJcwdcjExample tbJcJcwdcjExample = new TbJcJcwdcjExample();
            tbJcJcwdcjExample.createCriteria().andYwsjgxsjEqualTo(tbBlMzblgy.getYwsjgxsj()).andJzlshEqualTo(tbBlMzblgy.getJzlsh());
            tbJcJcwdcjMapper.deleteByExample(tbJcJcwdcjExample);

            tbJcJcwdcjMapper.insertSelective(tbBlMzblgy);
        }
    }

    @DataSource(MASTER)
    public void insertJywdcj(List<DmsValueVo.Jywdcj> jywdcjList) {
        List<TbJyJywdcj> tbBlMzblgies = BeanUtil.copyToList(jywdcjList, TbJyJywdcj.class);
        for (TbJyJywdcj tbBlMzblgy : tbBlMzblgies) {
            tbJyJywdcjMapper.insertSelective(tbBlMzblgy);
        }
    }

    @DataSource(MASTER)
    public void insertYmhgb(List<DmsValueVo.Ymjgb> ymjgbList) {
        List<TbJyYmjgb> tbBlMzblgies = BeanUtil.copyToList(ymjgbList, TbJyYmjgb.class);
        for (TbJyYmjgb tbBlMzblgy : tbBlMzblgies) {
            tbJyYmjgbMapper.insertSelective(tbBlMzblgy);
        }
    }

    @DataSource(MASTER)
    public void insertXjjgb(List<DmsValueVo.Xjjgb> xjjgbList) {
        List<TbJyXjjgb> tbBlMzblgies = BeanUtil.copyToList(xjjgbList, TbJyXjjgb.class);
        for (TbJyXjjgb tbBlMzblgy : tbBlMzblgies) {
            tbJyXjjgbMapper.insertSelective(tbBlMzblgy);
        }
    }

    @DataSource(MASTER)
    public void insertJyjgzbb(List<DmsValueVo.Jyjgzbb> jyjgzbbList) {
        List<TbJyJyjgzbb> tbBlMzblgies = BeanUtil.copyToList(jyjgzbbList, TbJyJyjgzbb.class);
        for (TbJyJyjgzbb tbBlMzblgy : tbBlMzblgies) {
            tbJyJyjgzbbMapper.insertSelective(tbBlMzblgy);
        }
    }

    @DataSource(MASTER)
    public void insertJybgzb(List<DmsValueVo.Jybgzb> jybgzbList) {
        List<TbJyJybgzb> tbBlMzblgies = BeanUtil.copyToList(jybgzbList, TbJyJybgzb.class);
        for (TbJyJybgzb tbBlMzblgy : tbBlMzblgies) {
            tbJyJybgzbMapper.insertSelective(tbBlMzblgy);
        }
    }

    @DataSource(MASTER)
    public void insertCyxj(List<DmsValueVo.Cyxj> cyxjList) {
        List<TbBlCyxj> tbBlMzblgies = BeanUtil.copyToList(cyxjList, TbBlCyxj.class);
        for (TbBlCyxj tbBlMzblgy : tbBlMzblgies) {
            tbBlCyxjMapper.insertSelective(tbBlMzblgy);
        }
    }

    @DataSource(MASTER)
    public void insertLcljjxx(List<DmsValueVo.Lcljjlxx> lcljjlxxList) {
        List<TbBlLcljjlxx> tbBlMzblgies = BeanUtil.copyToList(lcljjlxxList, TbBlLcljjlxx.class);
        for (TbBlLcljjlxx tbBlMzblgy : tbBlMzblgies) {
            tbBlLcljjlxxMapper.insertSelective(tbBlMzblgy);
        }
    }

    @DataSource(MASTER)
    public void insertScbcjlxx(List<DmsValueVo.Scbcjlxx> scbcjlxxList) {
        List<TbBlScbcjlxx> tbBlMzblgies = BeanUtil.copyToList(scbcjlxxList, TbBlScbcjlxx.class);
        for (TbBlScbcjlxx tbBlMzblgy : tbBlMzblgies) {
            tbBlScbcjlxxMapper.insertSelective(tbBlMzblgy);
        }
    }

    @DataSource(MASTER)
    public void insertHrcyjlxx(List<DmsValueVo.Hrcyjlxx> hrcyjlxxList) {
        List<TbBl24Hrcyjlxx> tbBlMzblgies = BeanUtil.copyToList(hrcyjlxxList, TbBl24Hrcyjlxx.class);
        for (TbBl24Hrcyjlxx tbBlMzblgy : tbBlMzblgies) {
            tbBl24HrcyjlxxMapper.insertSelective(tbBlMzblgy);
        }
    }

    @DataSource(MASTER)
    public void insertGzhcsyjhc(List<DmsValueVo.Gzhcsyjlhc> gzhcsyjlhcList) {
        List<TbBlGzhcsyjlhc> tbBlMzblgies = BeanUtil.copyToList(gzhcsyjlhcList, TbBlGzhcsyjlhc.class);
        for (TbBlGzhcsyjlhc tbBlMzblgy : tbBlMzblgies) {
            tbBlGzhcsyjlhcMapper.insertSelective(tbBlMzblgy);
        }
    }

    @DataSource(MASTER)
    public void insertGzhcsyjlxx(List<DmsValueVo.Gzhcsyjlxx> gzhcsyjlxxList) {
        List<TbBlGzhcsyjlxx> tbBlMzblgies = BeanUtil.copyToList(gzhcsyjlxxList, TbBlGzhcsyjlxx.class);
        for (TbBlGzhcsyjlxx tbBlMzblgy : tbBlMzblgies) {
            tbBlGzhcsyjlxxMapper.insertSelective(tbBlMzblgy);
        }
    }

    @DataSource(MASTER)
    public void insertSwjlxx(List<DmsValueVo.Swjlxx> swjlxxList) {
        List<TbBlSwjlxx> tbBlMzblgies = BeanUtil.copyToList(swjlxxList, TbBlSwjlxx.class);
        for (TbBlSwjlxx tbBlMzblgy : tbBlMzblgies) {
            tbBlSwjlxxMapper.insertSelective(tbBlMzblgy);
        }
    }

    @Override
    public void insertTbZyJsmxbs(List<DmsValueVo.TbZyJsmxb> tbZyJsmxbs) {
        List<TbZyJsmxb> tbZyJsmxbList = BeanUtil.copyToList(tbZyJsmxbs, TbZyJsmxb.class);
        for (TbZyJsmxb tbBlMzblgy : tbZyJsmxbList) {
            tbZyJsmxbMapper.insertSelective(tbBlMzblgy);
        }
    }

    @DataSource(MASTER)
    public void insertRybljl(List<DmsValueVo.Rybljl> rybljlList) {
        List<TbBlRybljlWithBLOBs> tbBlMzblgies = BeanUtil.copyToList(rybljlList, TbBlRybljlWithBLOBs.class);
        for (TbBlRybljlWithBLOBs tbBlMzblgy : tbBlMzblgies) {
            tbBlRybljlMapper.insertSelective(tbBlMzblgy);
        }
    }

    @DataSource(MASTER)
    public void insertZyblgy(List<DmsValueVo.Zyblgy> zyblgyList) {
        List<TbBlMzblgy> tbBlMzblgies = BeanUtil.copyToList(zyblgyList, TbBlMzblgy.class);
        for (TbBlMzblgy tbBlMzblgy : tbBlMzblgies) {
            tbBlMzblgyMapper.insertSelective(tbBlMzblgy);
        }
    }

    @DataSource(MASTER)
    public List<PmsPatient> insertHzxx(List<TbHzxxb> hzxxbListh) {
        List<PmsPatient> patientList = new ArrayList<>();
        for (TbHzxxb tbBlMzblgy : hzxxbListh) {
            if (tbBlMzblgy != null && tbBlMzblgy.getXm() != null && StrUtil.isNotEmpty(tbBlMzblgy.getSfzhm())) {
                //人口学
                tbHzxxbMapper.insertSelective(tbBlMzblgy);
                patientList.add(insertPatient(tbBlMzblgy));
            }
        }
        return patientList;
    }

    @Override
    @DataSource(MASTER)
    public void insertTbZyRydjmxb(List<TbZyRydjmxb> tbZyRydjmxbs) {
        for (TbZyRydjmxb tbZyRydjmxb : tbZyRydjmxbs) {
            if (tbZyRydjmxb == null) {
                continue;
            }
            tbZyRydjmxbMapper.insert(tbZyRydjmxb);
        }
    }

    @DataSource(MASTER)
    public void insertSsxx(List<DmsValueVo.Ssxx> ssxxList) {
        List<TbBaSsxx> tbBlMzblgies = BeanUtil.copyToList(ssxxList, TbBaSsxx.class);
        for (TbBaSsxx tbBlMzblgy : tbBlMzblgies) {
            tbBaSsxxMapper.insertSelective(tbBlMzblgy);
        }
    }

    @DataSource(MASTER)
    public PmsPatient insertPatient(TbHzxxb hzxx) {
        PmsPatientExample pmsPatientExample = new PmsPatientExample();
        if (StrUtil.isEmpty(hzxx.getSfzhm())) {
            return null;
        }
        pmsPatientExample.createCriteria().andIdCardEqualTo(hzxx.getSfzhm()).andDelFlagEqualTo((byte) 0);
        List<PmsPatient> pmsPatients = pmsPatientMapper.selectByExample(pmsPatientExample);
        if (CollectionUtil.isNotEmpty(pmsPatients)) {
            PmsPatient pmsPatient = pmsPatients.get(0);
            pmsPatient.setAge(IdcardUtil.getAgeByIdCard(hzxx.getSfzhm()));
            pmsPatient.setName(hzxx.getXm());
            pmsPatient.setPinyinFull(PinyinUtil.getPinyin(hzxx.getXm()));
            pmsPatient.setPinyinSimple(PinyinUtil.getFirstLetter(hzxx.getXm(), ""));
            pmsPatient.setSex(Byte.parseByte(hzxx.getXb()));
            pmsPatient.setPhone(hzxx.getLxrdh());
            pmsPatient.setRegisteredCity(hzxx.getHkdzB());
            pmsPatient.setIdCard(hzxx.getSfzhm());
            pmsPatient.setRegisteredProvince(hzxx.getHkdzA());
            pmsPatient.setPostalCode(hzxx.getHkdzyb());
            pmsPatient.setGrbslx(hzxx.getGrbslx());
            pmsPatient.setGrbsm(hzxx.getGrbsm());
            pmsPatient.setSource(2);
            pmsPatient.setRegisteredCityDetail(hzxx.getHkdzE());
            pmsPatient.setCreateCompany("");
            pmsPatientMapper.updateByPrimaryKeySelective(pmsPatient);
            return pmsPatient;
        } else {
            PmsPatient pmsPatient = new PmsPatient();
            if (StrUtil.isNotEmpty(hzxx.getSfzhm())) {
                pmsPatient.setAge(IdcardUtil.getAgeByIdCard(hzxx.getSfzhm()));
            }
            pmsPatient.setName(hzxx.getXm());
            pmsPatient.setGrbslx(hzxx.getGrbslx());
            pmsPatient.setGrbsm(hzxx.getGrbsm());
            pmsPatient.setPinyinFull(PinyinUtil.getPinyin(hzxx.getXm()));
            pmsPatient.setIdCard(hzxx.getSfzhm());
            pmsPatient.setPinyinSimple(PinyinUtil.getFirstLetter(hzxx.getXm(), ""));
            pmsPatient.setSex(Byte.parseByte(hzxx.getXb()));
            pmsPatient.setPhone(hzxx.getLxrdh());
            pmsPatient.setSource(2);
            pmsPatient.setRegisteredCity(hzxx.getHkdzB());
            pmsPatient.setId(IdUtil.getSnowflake().nextId());
            pmsPatient.setRegisteredProvince(hzxx.getHkdzA());
            pmsPatient.setPostalCode(hzxx.getHkdzyb());
            pmsPatient.setRegisteredCityDetail(hzxx.getHkdzE());
            pmsPatient.setDelFlag((byte) 0);
            pmsPatientMapper.insertSelective(pmsPatient);
            return pmsPatient;
        }

    }


    @DataSource(MASTER)
    public void insertZdxx(List<DmsValueVo.Zdxx> zdxxList) {
        List<TbBaZdxx> tbBlMzblgies = BeanUtil.copyToList(zdxxList, TbBaZdxx.class);
        for (TbBaZdxx tbBlMzblgy : tbBlMzblgies) {
            tbBaZdxxMapper.insertSelective(tbBlMzblgy);
        }
    }

    public DmsRunningLog newDcsRunningLog(Byte dcsRunningStatus) {
        DmsRunningLog dcsRunningLog = new DmsRunningLog();
        dcsRunningLog.setId(IdUtil.getSnowflake().nextId());
        dcsRunningLog.setStartTime(DateUtil.date());
        dcsRunningLog.setEndTime(null);
        dcsRunningLog.setSyncRecords(0L);
        dcsRunningLog.setSyncSeconds(0L);
        dcsRunningLog.setStatus(dcsRunningStatus);
        return dcsRunningLog;
    }

    @Override
    @DataSource(DataSourceType.MASTER)
    public boolean isProcessing() {
        DmsRunningLogExample example = new DmsRunningLogExample();
        example.createCriteria().andStatusEqualTo(UserConstants.DcsRunningStatus.Processing.getCode().byteValue());
        return dmsRunningLogMapper.countByExample(example) > 0;
    }


    @Override
    @DataSource(DataSourceType.MASTER)
    public Map<String, Long> deleteDmsPatient(List<DmsValueVo.PatientInfo> patientInfoList) {
        Map<String, Long> map = new HashMap<>();
        if (CollectionUtil.isEmpty(patientInfoList)) {
            return map;
        }
        dmsPatientMapper.deleteByExample(new DmsPatientExample());
        log.info("pati:{}", patientInfoList);
        for (DmsValueVo.PatientInfo patientInfo : patientInfoList) {
            DmsPatient dmsPatient = new DmsPatient();
            BeanUtil.copyProperties(patientInfo, dmsPatient);
            dmsPatient.setId(IdUtil.getSnowflake().nextId());
            dmsPatient.setSfzhm(patientInfo.getSfzhm());
            dmsPatient.setJzksrq(patientInfo.getJzksrq());
            dmsPatient.setXm(patientInfo.getXm());
            dmsPatient.setSsy(patientInfo.getSsy());
            dmsPatient.setSzy(patientInfo.getSzy());
            dmsPatient.setJzlsh(patientInfo.getJzlsh());
            dmsPatient.setSfjz(patientInfo.getSfjz());
            dmsPatient.setCreateTime(new Date());
            dmsPatient.setDelFlag((byte) 0);
            log.info("dmsPatient:{}, {}", dmsPatient, patientInfo.getSfzhm());
            dmsPatientMapper.insertSelective(dmsPatient);

            //解析治疗档案数据
            PmsArchives pmsArchives = new PmsArchives();
            if (IdcardUtil.isValidCard(patientInfo.getSfzhm())) {
                pmsArchives.setIdCard(patientInfo.getSfzhm());
                pmsArchives.setAge(IdcardUtil.getAgeByIdCard(patientInfo.getSfzhm()));
                pmsArchives.setSex((byte) IdcardUtil.getGenderByIdCard(patientInfo.getSfzhm()));
            }
            if (StrUtil.isNotEmpty(patientInfo.getXm())) {
                pmsArchives.setPinyinFull(PinyinUtil.getPinyin(patientInfo.getXm()));
                pmsArchives.setPinyinSimple(PinyinUtil.getFirstLetter(patientInfo.getXm(), ""));
            }
            pmsArchives.setId(IdUtil.getSnowflake().nextId());
            pmsArchives.setType((byte) 2);
            pmsArchives.setDelFlag((byte) 0);
            pmsArchives.setName(patientInfo.getXm());
            pmsArchives.setCreateTime(new Date());
            pmsArchives.setSource(2);
            pmsArchives.setWeight(patientInfo.getTz());
            pmsArchives.setRemark(patientInfo.getJzlsh());
            pmsArchives.setHeight(patientInfo.getSg());
            if (StrUtil.isNotEmpty(patientInfo.getTz()) && StrUtil.isNotEmpty(patientInfo.getSg())) {
                BigDecimal decimal = new BigDecimal(patientInfo.getTz()).divide(new BigDecimal(patientInfo.getSg()), RoundingMode.CEILING).setScale(BigDecimal.ROUND_HALF_UP).divide(new BigDecimal(patientInfo.getSg()), RoundingMode.CEILING).setScale(BigDecimal.ROUND_HALF_UP);

                pmsArchives.setBmi(decimal + "");
            }
            pmsArchivesMapper.insertSelective(pmsArchives);
            if (StrUtil.isNotEmpty(patientInfo.getJzksrq())) {
//                PmsArchivesRecord pmsArchivesRecord = new PmsArchivesRecord();
//                pmsArchivesRecord.setAnswer(patientInfo.getJzksrq());
//                pmsArchivesRecord.setId(IdUtil.getSnowflake().nextId());
//                pmsArchivesRecord.setCode("JBXX-DY-TIME");
//                pmsArchivesRecord.setDelFlag((byte) 0);
//                pmsArchivesRecord.setArchivesId(pmsArchives.getId());
//                pmsArchivesRecord.setCreatedAt(new Date());
//                pmsArchivesRecordMapper.insertSelective(pmsArchivesRecord);

                AmsFirstAidDto.SaveAidRecord saveAidRecord = new AmsFirstAidDto.SaveAidRecord();
                saveAidRecord.setFirstAidId(pmsArchives.getId());
                AmsFirstAidDto.SaveAidRecord.CodeAndAnswer codeAndAnswer = new AmsFirstAidDto.SaveAidRecord.CodeAndAnswer();
                codeAndAnswer.setAnswer(CollectionUtil.newArrayList(patientInfo.getJzksrq()));
                codeAndAnswer.setTime(new Date());
                codeAndAnswer.setQuestionCode("JBXX-DY-TIME");
                saveAidRecord.setCodeAndAnswerList(CollectionUtil.newArrayList(codeAndAnswer));
                try {
                    amsFirstAidService.saveAidRecord(saveAidRecord);
                } catch (Exception e) {
                    log.error("添加到院时间报错:{}", pmsArchives.getId());
                }

            }
            if (StrUtil.isNotEmpty(patientInfo.getJzlsh())) {
//                PmsArchivesRecord pmsArchivesRecord = new PmsArchivesRecord();
//                pmsArchivesRecord.setAnswer(patientInfo.getJzlsh());
//                pmsArchivesRecord.setId(IdUtil.getSnowflake().nextId());
//                pmsArchivesRecord.setCode("ZL-JBXX-ZYH");
//                pmsArchivesRecord.setDelFlag((byte) 0);
//                pmsArchivesRecord.setCreatedAt(new Date());
//                pmsArchivesRecordMapper.insertSelective(pmsArchivesRecord);

                AmsFirstAidDto.SaveAidRecord saveAidRecord = new AmsFirstAidDto.SaveAidRecord();
                saveAidRecord.setFirstAidId(pmsArchives.getId());
                AmsFirstAidDto.SaveAidRecord.CodeAndAnswer codeAndAnswer = new AmsFirstAidDto.SaveAidRecord.CodeAndAnswer();
                codeAndAnswer.setAnswer(CollectionUtil.newArrayList(patientInfo.getJzlsh()));
                codeAndAnswer.setTime(new Date());
                codeAndAnswer.setQuestionCode("ZL-JBXX-ZYH");
                saveAidRecord.setCodeAndAnswerList(CollectionUtil.newArrayList(codeAndAnswer));
                try {
                    amsFirstAidService.saveAidRecord(saveAidRecord);
                } catch (Exception e) {
                    log.error("添加住院号报错:{}", pmsArchives.getId());
                }
            }
            if (StrUtil.isNotEmpty(patientInfo.getZfy())) {
//                PmsArchivesRecord pmsArchivesRecord = new PmsArchivesRecord();
//                pmsArchivesRecord.setAnswer(patientInfo.getZfy());
//                pmsArchivesRecord.setId(IdUtil.getSnowflake().nextId());
//                pmsArchivesRecord.setCode("ZL-JBXX-ZYZFY");
//                pmsArchivesRecord.setDelFlag((byte) 0);
//                pmsArchivesRecord.setCreatedAt(new Date());
//                pmsArchivesRecordMapper.insertSelective(pmsArchivesRecord);

                AmsFirstAidDto.SaveAidRecord saveAidRecord = new AmsFirstAidDto.SaveAidRecord();
                saveAidRecord.setFirstAidId(pmsArchives.getId());
                AmsFirstAidDto.SaveAidRecord.CodeAndAnswer codeAndAnswer = new AmsFirstAidDto.SaveAidRecord.CodeAndAnswer();
                codeAndAnswer.setAnswer(CollectionUtil.newArrayList(patientInfo.getZfy()));
                codeAndAnswer.setTime(new Date());
                codeAndAnswer.setQuestionCode("ZL-JBXX-ZYZFY");
                saveAidRecord.setCodeAndAnswerList(CollectionUtil.newArrayList(codeAndAnswer));
                try {
                    amsFirstAidService.saveAidRecord(saveAidRecord);
                } catch (Exception e) {
                    log.error("添加住院总费用报错:{}", pmsArchives.getId());
                }
            }
            if (StrUtil.isNotEmpty(patientInfo.getLyfsdm())) {
                PmsArchivesRecord pmsArchivesRecord = new PmsArchivesRecord();
                pmsArchivesRecord.setAnswer(patientInfo.getLyfsdm());
                pmsArchivesRecord.setId(IdUtil.getSnowflake().nextId());
                pmsArchivesRecord.setCode("ZL-JBXX-LYFS");
                pmsArchivesRecord.setDelFlag((byte) 0);
                pmsArchivesRecord.setCreatedAt(new Date());
                pmsArchivesRecordMapper.insertSelective(pmsArchivesRecord);

                AmsFirstAidDto.SaveAidRecord saveAidRecord = new AmsFirstAidDto.SaveAidRecord();
                saveAidRecord.setFirstAidId(pmsArchives.getId());
                AmsFirstAidDto.SaveAidRecord.CodeAndAnswer codeAndAnswer = new AmsFirstAidDto.SaveAidRecord.CodeAndAnswer();
                codeAndAnswer.setAnswer(CollectionUtil.newArrayList(patientInfo.getLyfsdm()));
                codeAndAnswer.setTime(new Date());
                codeAndAnswer.setQuestionCode("ZL-JBXX-LYFS");
                saveAidRecord.setCodeAndAnswerList(CollectionUtil.newArrayList(codeAndAnswer));
                try {
                    amsFirstAidService.saveAidRecord(saveAidRecord);
                } catch (Exception e) {
                    log.error("添加住院号报错:{}", pmsArchives.getId());
                }
            }
            if (StrUtil.isNotEmpty(patientInfo.getJzksrq())) {
                PmsArchivesRecord pmsArchivesRecord = new PmsArchivesRecord();
                pmsArchivesRecord.setAnswer(patientInfo.getJzksrq());
                pmsArchivesRecord.setId(IdUtil.getSnowflake().nextId());
                pmsArchivesRecord.setCode("ZL-FBQK-DY-TIME");
                pmsArchivesRecord.setDelFlag((byte) 0);
                pmsArchivesRecord.setCreatedAt(new Date());
                pmsArchivesRecordMapper.insertSelective(pmsArchivesRecord);
            }
            if (StrUtil.isNotEmpty(patientInfo.getSsy())) {
                PmsArchivesRecord pmsArchivesRecord = new PmsArchivesRecord();
                pmsArchivesRecord.setAnswer(patientInfo.getSsy());
                pmsArchivesRecord.setId(IdUtil.getSnowflake().nextId());
                pmsArchivesRecord.setCode("ZL-RYPG-SBP");
                pmsArchivesRecord.setDelFlag((byte) 0);
                pmsArchivesRecord.setCreatedAt(new Date());
                pmsArchivesRecordMapper.insertSelective(pmsArchivesRecord);
            }
            if (StrUtil.isNotEmpty(patientInfo.getSzy())) {
                PmsArchivesRecord pmsArchivesRecord = new PmsArchivesRecord();
                pmsArchivesRecord.setAnswer(patientInfo.getSzy());
                pmsArchivesRecord.setId(IdUtil.getSnowflake().nextId());
                pmsArchivesRecord.setCode("ZL-RYPG-DBP");
                pmsArchivesRecord.setDelFlag((byte) 0);
                pmsArchivesRecord.setCreatedAt(new Date());
                pmsArchivesRecordMapper.insertSelective(pmsArchivesRecord);
            }
            map.put(patientInfo.getJzlsh(), pmsArchives.getId());
        }
        return map;
    }

    @Override
    @DataSource(DataSourceType.MASTER)
    public void deleteDmsCy(List<DmsValueVo.CyInfo> cyInfo, Map<String, Long> map) {
        if (CollectionUtil.isEmpty(cyInfo)) {
            return;
        }
        dmsCyMapper.deleteByExample(new DmsCyExample());
        for (DmsValueVo.CyInfo info : cyInfo) {
            Long patientId = map.get(info.getJzlsh());

            DmsCy dmsCy = new DmsCy();
            BeanUtil.copyProperties(info, dmsCy);
            dmsCy.setId(IdUtil.getSnowflake().nextId());
            dmsCy.setCreateTime(new Date());
            dmsCy.setDelFlag((byte) 0);
            dmsCy.setPatientId(patientId);
            dmsCyMapper.insertSelective(dmsCy);

            PmsArchivesRecord pmsArchivesRecord = new PmsArchivesRecord();
            pmsArchivesRecord.setDelFlag((byte) 0);
            pmsArchivesRecord.setArchivesId(patientId);
            pmsArchivesRecord.setCreatedAt(new Date());

            if (StrUtil.isNotEmpty(info.getCysj())) {
                pmsArchivesRecord.setCode("ZL-JBXX-CYRQ");
                pmsArchivesRecord.setAnswer(info.getCysj());
                pmsArchivesRecord.setId(IdUtil.getSnowflake().nextId());
                pmsArchivesRecordMapper.insertSelective(pmsArchivesRecord);
            }
            //住院天数
            if (StrUtil.isNotEmpty(info.getSjzyts())) {
                pmsArchivesRecord.setCode("ZL-JBXX-ZYTS");
                pmsArchivesRecord.setAnswer(info.getSjzyts());
                pmsArchivesRecord.setId(IdUtil.getSnowflake().nextId());
                pmsArchivesRecordMapper.insertSelective(pmsArchivesRecord);
            }
            //死亡日期
            if (StrUtil.isNotEmpty(info.getSwrqsj())) {
                pmsArchivesRecord.setCode("ZL-JBXX-DIE-T");
                pmsArchivesRecord.setAnswer(info.getSwrqsj());
                pmsArchivesRecord.setId(IdUtil.getSnowflake().nextId());
                pmsArchivesRecordMapper.insertSelective(pmsArchivesRecord);

                //离院方式 死亡
                pmsArchivesRecord.setCode("ZL-JBXX-LYFS");
                pmsArchivesRecord.setAnswer("5");
                pmsArchivesRecord.setId(IdUtil.getSnowflake().nextId());
                pmsArchivesRecordMapper.insertSelective(pmsArchivesRecord);
            }
        }
    }

    @Override
    @DataSource(DataSourceType.MASTER)
    public void deleteDmsZdxx(List<DmsValueVo.ZdxxInfo> zdxx, Map<String, Long> map) {
        if (CollectionUtil.isEmpty(zdxx)) {
            return;
        }
        dmsZdxxMapper.deleteByExample(new DmsZdxxExample());
        for (DmsValueVo.ZdxxInfo zdxxInfo : zdxx) {
            DmsZdxx dmsZdxx = new DmsZdxx();
            BeanUtil.copyProperties(zdxxInfo, dmsZdxx);
            dmsZdxx.setId(IdUtil.getSnowflake().nextId());
            dmsZdxx.setCreateTime(new Date());
            dmsZdxx.setDelFlag((byte) 0);
            dmsZdxxMapper.insertSelective(dmsZdxx);

            Long patientId = map.get(zdxxInfo.getJzlsh());
            PmsArchivesRecord pmsArchivesRecord = new PmsArchivesRecord();
            pmsArchivesRecord.setDelFlag((byte) 0);
            pmsArchivesRecord.setArchivesId(patientId);
            pmsArchivesRecord.setCreatedAt(new Date());
            //诊断主次
            if (StrUtil.isNotEmpty(zdxxInfo.getZdzcbm())) {
                if (zdxxInfo.getZdzcbm().equals("01")) {
                    pmsArchivesRecord.setCode("ZL-JBXX-ZDBM");
                    pmsArchivesRecord.setAnswer(zdxxInfo.getZdbm());
                    pmsArchivesRecord.setId(IdUtil.getSnowflake().nextId());
                    pmsArchivesRecordMapper.insertSelective(pmsArchivesRecord);

                    pmsArchivesRecord.setCode("ZL-JBXX-ZDMC");
                    pmsArchivesRecord.setAnswer(zdxxInfo.getZdmc());
                    pmsArchivesRecord.setId(IdUtil.getSnowflake().nextId());
                    pmsArchivesRecordMapper.insertSelective(pmsArchivesRecord);
                }
            }
            if (StrUtil.isNotEmpty(zdxxInfo.getFbrqsj())) {
//                pmsArchivesRecord.setCode("ZL-FBQK-NCX-FBSJ-IS");
//                pmsArchivesRecord.setAnswer("1");
//                pmsArchivesRecord.setId(IdUtil.getSnowflake().nextId());
//                pmsArchivesRecordMapper.insertSelective(pmsArchivesRecord);

                pmsArchivesRecord.setCode("ZL-FBQK-FBSJ");
                pmsArchivesRecord.setAnswer(zdxxInfo.getFbrqsj());
                pmsArchivesRecord.setId(IdUtil.getSnowflake().nextId());
                pmsArchivesRecordMapper.insertSelective(pmsArchivesRecord);
            }

            if (StrUtil.isNotEmpty(zdxxInfo.getRylx())) {
                pmsArchivesRecord.setCode("ZL-JBXX-RYTJ");
                pmsArchivesRecord.setAnswer(zdxxInfo.getRylx());
                pmsArchivesRecord.setId(IdUtil.getSnowflake().nextId());
                pmsArchivesRecordMapper.insertSelective(pmsArchivesRecord);
            }

        }
    }

    @Override
    @DataSource(DataSourceType.MASTER)
    public void deleteDmsSsjl(List<DmsValueVo.SsjlDto> ssjlList, Map<String, Long> map) {
        if (CollectionUtil.isEmpty(ssjlList)) {
            return;
        }
        dmsSsjlMapper.deleteByExample(new DmsSsjlExample());
        for (DmsValueVo.SsjlDto ssjlDto : ssjlList) {
            Long patientId = map.get(ssjlDto.getJzlsh());
            DmsSsjl dmsSsjl = new DmsSsjl();
            BeanUtil.copyProperties(ssjlDto, dmsSsjl);
            dmsSsjl.setId(IdUtil.getSnowflake().nextId());
            dmsSsjl.setCreateTime(new Date());
            dmsSsjl.setDelFlag((byte) 0);
            dmsSsjl.setPatientId(patientId);
            dmsSsjlMapper.insertSelective(dmsSsjl);

            //解析成平台数据
            //ZL-YWBDJ-SS-TIME
            PmsArchivesRecord pmsArchivesRecord = new PmsArchivesRecord();
            pmsArchivesRecord.setDelFlag((byte) 0);
            pmsArchivesRecord.setArchivesId(patientId);
            pmsArchivesRecord.setCreatedAt(new Date());
            if (StrUtil.isNotEmpty(dmsSsjl.getMzff())) {
                pmsArchivesRecord.setCode("ZL-NCX-MZ");
                pmsArchivesRecord.setAnswer(dmsSsjl.getMzff());
                pmsArchivesRecord.setId(IdUtil.getSnowflake().nextId());
                pmsArchivesRecordMapper.insertSelective(pmsArchivesRecord);
            }
            if (StrUtil.isNotEmpty(dmsSsjl.getSsrq())) {
                pmsArchivesRecord.setCode("ZL-NCX-T-SS");
                pmsArchivesRecord.setAnswer(dmsSsjl.getSsrq());
                pmsArchivesRecord.setId(IdUtil.getSnowflake().nextId());
                pmsArchivesRecordMapper.insertSelective(pmsArchivesRecord);
            }
            if (StrUtil.isNotEmpty(dmsSsjl.getSsczdm())) {
                pmsArchivesRecord.setCode("ZL-NCX-SS");
                pmsArchivesRecord.setAnswer(dmsSsjl.getSsczdm());
                pmsArchivesRecord.setId(IdUtil.getSnowflake().nextId());
                pmsArchivesRecordMapper.insertSelective(pmsArchivesRecord);
            }
        }
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.SsjlDto> ssjl() {
        return dmsValueDao.getDmsValue();
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.PatientInfo> getPatientInfo(List<String> hzlshList) {
        List<DmsValueVo.PatientInfo> patientInfo = dmsValueDao.getPatientInfo(hzlshList);
        log.info("patientInfo:{}", patientInfo);
        return patientInfo;
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.ZdxxInfo> getZdxx(List<String> hzlshList) {
        return dmsValueDao.getZdxx(hzlshList);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.CyInfo> getCyInfo(List<String> hzlshList) {
        return dmsValueDao.getCyInfo(hzlshList);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.Yxjcjl> getCt(List<String> hzlshList) {
        return dmsValueDao.getCt(hzlshList);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.Yizhu> getYizhu(List<String> hzlshList) {
        return dmsValueDao.getYizhu(hzlshList);
    }

    @Override
    public void deleteCt(List<DmsValueVo.Yxjcjl> ct, Map<String, Long> map) {
        if (CollectionUtil.isEmpty(ct)) {
            return;
        }
        ctMapper.deleteByExample(new DmsCtExample());
        for (DmsValueVo.Yxjcjl ssjlDto : ct) {
            Long patientId = map.get(ssjlDto.getJzlsh());
            DmsCt dmsCt = new DmsCt();
            BeanUtil.copyProperties(ssjlDto, dmsCt);
            dmsCt.setId(IdUtil.getSnowflake().nextId());
            dmsCt.setCreateTime(new Date());
            dmsCt.setDelFlag((byte) 0);
            dmsCt.setPatientId(patientId);
            ctMapper.insertSelective(dmsCt);
        }
    }

    @Override
    public void deleteJianyan(List<DmsValueVo.JianYan> jianYanList, Map<String, Long> map) {
        if (CollectionUtil.isEmpty(jianYanList)) {
            return;
        }
        dmsJianyanMapper.deleteByExample(new DmsJianyanExample());
        for (DmsValueVo.JianYan jianYan : jianYanList) {
            Long patientId = map.get(jianYan.getJzlsh());
            DmsJianyan dmsJianyan = new DmsJianyan();
            BeanUtil.copyProperties(jianYan, dmsJianyan);
            dmsJianyan.setId(IdUtil.getSnowflake().nextId());
            dmsJianyan.setCreateTime(new Date());
            dmsJianyan.setDelFlag((byte) 0);
            dmsJianyan.setPatientId(patientId);
            dmsJianyanMapper.insertSelective(dmsJianyan);
        }
    }

    @Override
    public void deleteYizhu(List<DmsValueVo.Yizhu> ct, Map<String, Long> map) {
//        if (CollectionUtil.isEmpty(ct)) {
//            return;
//        }
//        yizhuMapper.deleteByExample(new DmsYizhuExample());
//
//        for (DmsValueVo.Yizhu yizhu : ct) {
//            Long patientId = map.get(yizhu.getJzlsh());
//            DmsYizhu dmsYizhu = new DmsYizhu();
//            BeanUtil.copyProperties(yizhu, dmsYizhu);
//            dmsYizhu.setId(IdUtil.getSnowflake().nextId());
//            dmsYizhu.setCreateTime(new Date());
//            dmsYizhu.setDelFlag((byte) 0);
//            dmsYizhu.setPatientId(patientId);
//            yizhuMapper.insertSelective(dmsYizhu);
//
//            AmsFirstAidDto.SaveAidRecord param = new AmsFirstAidDto.SaveAidRecord();
//            param.setFirstAidId(patientId);
//
////            PmsArchivesRecord pmsArchivesRecord = new PmsArchivesRecord();
////            pmsArchivesRecord.setDelFlag((byte) 0);
////            pmsArchivesRecord.setArchivesId(patientId);
////            pmsArchivesRecord.setCreatedAt(new Date());
//            if (StrUtil.isNotEmpty(yizhu.getJxdm())) {
//                AmsFirstAidDto.SaveAidRecord.CodeAndAnswer codeAndAnswer = new AmsFirstAidDto.SaveAidRecord.CodeAndAnswer();
//                codeAndAnswer.setQuestionCode(ZL_JRZT_YWZL_DUAL_YW.getKey());
//                codeAndAnswer.setAnswer(CollectionUtil.newArrayList(yizhu.getJxdm()));
////                pmsArchivesRecord.setCode("ZL-JRZT-YWZL-DUAL-YW");
////                pmsArchivesRecord.setAnswer(yizhu.getJxdm());
////                pmsArchivesRecord.setId(IdUtil.getSnowflake().nextId());
//                param.setCodeAndAnswerList(CollectionUtil.newArrayList(codeAndAnswer));
//                try {
//                    amsFirstAidService.saveAidRecord(param);
//                } catch (Exception e) {
//                    log.error("添加使用的药物错误");
//                }
//            }
//        }
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.JianYan> getJianYan(List<String> hzlshList) {
        return dmsValueDao.getJianyan(hzlshList);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<DmsValueVo.Rs> getRs(List<String> hzlshList) {
        return dmsValueDao.getRs(hzlshList);
    }


}
